package com.lmm.rpc.core.cluster;

import java.util.ArrayList;
import java.util.List;

/**
 * @title: AbstractLoadBalancer
 * @Description: 负载抽象类，抽取的模板方法
 * @Author Lmm
 * @Date: 2022/9/29 15:04
 * @Version 1.0
 */
public abstract class AbstractLoadBalancer  implements ILoadBalancer{
    @Override
    public RemoteServerInfo select(List<RemoteServerInfo> list) {
        if (list == null || list.size() == 0)
            return null;
        if (list.size() == 1)
            return list.get(0);
        List<RemoteServerInfo> enableList = new ArrayList<>();
        for (RemoteServerInfo remoteServer : list) {
            if (remoteServer.isEnable()){
                enableList.add(remoteServer);
            }
        }
        return doSelect(enableList);
    }

    /**
     * 具体的负载策略，由子类去实现
     * */
    protected abstract RemoteServerInfo doSelect(List<RemoteServerInfo> enableList) ;
}
